package com.stylefeng.guns.modular.wz.util;

import com.alibaba.druid.util.StringUtils;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.InputStream;
import java.util.Properties;

/**
 * 推送的OpenAPI文档 https://help.aliyun.com/document_detail/mobilepush/api-reference/openapi.html
 */
public class AliBaseUtil {

    protected static String region;
    protected static long android_appKey;
    protected static long ios_appKey;
    protected static String deviceIds;
    protected static String deviceId;
    protected static String accounts;
    protected static String account;
    protected static String aliases;
    protected static String alias;
    protected static String tag;
    protected static String tagExpression;

    protected static DefaultAcsClient client;
    protected static Log logger = LogFactory.getLog(AliBaseUtil.class);

    /**
     * 从配置文件中读取配置值，初始化Client
     * <p>
     * 1. 如何获取 accessKeyId/accessKeySecret/appKey 照见README.md 中的说明<br/>
     * 2. 先在 push.properties 配置文件中 填入你的获取的值
     */
    static{
        InputStream inputStream = AliBaseUtil.class.getClassLoader().getResourceAsStream("aliyun.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            logger.info("aliyun配置文件加载成功~");
        }catch (Exception e){
            logger.info("aliyun配置文件加载失败~");
        }
        region = properties.getProperty("regionId");
        String accessKeyId = properties.getProperty("accessKeyId");
        if(StringUtils.isEmpty(accessKeyId)){
            logger.info("先在 aliyun.properties 配置文件中配置 accessKeyId");
        }
        String accessKeySecret = properties.getProperty("accessKeySecret");
        if(StringUtils.isEmpty(accessKeySecret)){
            logger.info("先在 aliyun.properties 配置文件中配置 accessKeySecret");
        }
        String android = properties.getProperty("android_appKey");
        if(StringUtils.isEmpty(android)){
            logger.info("先在 aliyun.properties 配置文件中配置 android_appKey");
        }else{
            android_appKey = Long.parseLong(android);
        }
        String ios = properties.getProperty("ios_appKey");
        if(StringUtils.isEmpty(ios)){
            logger.info("先在 aliyun.properties 配置文件中配置 ios_appKey");
        }else{
            ios_appKey = Long.parseLong(ios);
        }

//        appKey = Long.valueOf(key);
//        deviceIds = properties.getProperty("deviceIds");
//        deviceId = properties.getProperty("deviceId");
//        accounts = properties.getProperty("accounts");
//        account = properties.getProperty("account");
//        aliases = properties.getProperty("aliases");
//        alias = properties.getProperty("alias");
//        tag = properties.getProperty("tag");
//        tagExpression = properties.getProperty("tagExpression");
        IClientProfile profile = DefaultProfile.getProfile(region, accessKeyId, accessKeySecret);
        client = new DefaultAcsClient(profile);
    }
}
